package ${controllerPackage};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import ${packagePath}.common.page.PageTableHandler;
import ${packagePath}.common.page.PageTableRequest;
import ${packagePath}.common.result.ResultGenerator;
import ${servicePackage}.${className}Service;
import ${voPackage}.${className}VO;

@RestController
@RequestMapping("/${classname}s")
public class ${className}Controller {

    @Autowired
    private ${className}Service ${classname}Service;

    @PostMapping
    @PreAuthorize("hasAuthority('${classname}:add')")
    public Object save(@RequestBody ${className}VO ${classname}) {
        return ResultGenerator.genSuccessResult(${classname}Service.save(${classname}));
    }

    @GetMapping("/{id}")
    public Object findBy${pk.attrName}(@PathVariable ${pk.dataType} id) {
        return ResultGenerator.genSuccessResult(${classname}Service.findBy${pk.attrName}(id));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('${classname}:edit')")
    public Object update(@RequestBody ${className}VO ${classname}) {
        return ResultGenerator.genSuccessResult(${classname}Service.update(${classname}));
    }

    @GetMapping
    @PreAuthorize("hasAuthority('${classname}:query')")
    public Object list(PageTableRequest request) {
        return new PageTableHandler(req -> ${classname}Service.list(req.getParams(), req.getPageNum(), req.getPageSize())
        ).handle(request);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('${classname}:del')")
    public Object deleteBy${pk.attrName}(@PathVariable ${pk.attrType} id) {
        return ResultGenerator.genSuccessResult(${classname}Service.deleteBy${pk.attrName}(id));
    }

}
